<?php
/*
 * Web Gallery (FST)
 * Abe Yang <abeyang@cal.berkeley.edu> (c) 2008
 * http://code.google.com/p/flickrsetsbytag/
 *
/* ---------------------------------------------------------------------------------- */

require('fst.webgallery.php');

$fst = new FST();

$album_id = $_GET['album'];	// album id

// album view (show photos)
if ($album_id) {
	$type = 'showphotos';
	$tag = $_GET['tag'];
	$album = $fst->getAlbum($album_id, $tag);
	// TODO: check $album
	$title = htmlspecialchars($album['title']);
}
// album-list view (show albums)
else {
	$type = 'showalbums';
	$title = 'Albums';
}

$title .= ' ' . FST_WEBGALLERY_NAME;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

	<title><?php echo $title; ?></title>
	
	<link rel="stylesheet" href="webgallery.css" type="text/css" media="screen" charset="utf-8" />
	<!--[if IE]>
	<link rel="stylesheet" href="webgallery_ie.css" type="text/css" media="screen" charset="utf-8" />
	<![endif]-->
	
	<script src="../../commons/mootools.v1.11.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../mookit/mookit.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../commons/HistoryManager.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../commons/reflection.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../mookit/darkbox/darkbox.js" type="text/javascript" charset="utf-8"></script>
	<script src="webgallery.js" type="text/javascript" charset="utf-8"></script>
	
	<script type="text/javascript" charset="utf-8">
		window.addEvent('domready', function() {
			
			HistoryManager.initialize();

			<?php if ($type == 'showphotos') { ?>
				var json = <?php echo toJSON($album); ?>;
			 	var album = new Album(json, '<?php echo FST_ROOT_DIR; ?>');
			<? } else { ?>
				var albumslist = new AlbumsList({
					'title': '<?php echo WEB_NAME; ?> Web Gallery',
					'currenttag': '<?php echo FST_DEFAULT_TAG; ?>',
					'friendlyurl': '<?php echo FST_FRIENDLY_URL; ?>',
					'galleryurl': '<?php echo FST_ROOT_DIR ?>'
				});
				
				// start google analytics
				mookit.analytics('<?php echo FST_ROOT_DIR; ?>');
			<?php } // end if/else ?>
			
			HistoryManager.start();

			/* initialize web gallery settings */
			$('container').setHeight(['header', 'admin-console']);
			var margins = new Margins('<?php echo $type; ?>');
			margins.resizeAll();
			
			/* window resize */
			window.addEvent('resize', function() {
				$('container').setHeight(['header', 'admin-console']);
				<?php if ($type == 'showphotos') { ?>
				if (album.currentphoto) album.resize(album.currentphoto);
				<? }?>
				margins.resizeAll();
			});

		}); // end window.addEvent()
	</script>
</head>

<body id="<?php echo $type; ?>">
<?php
if ($_GET['debug'] && $album_id) {
	$fst->showAdminConsole($album_id, $tag, $_GET['type'], true);
} 
?>
<div id="header">
	<div id="topbar">
		<a id="buttonright" class="button" href="/">Return to <?php echo WEB_NAME ?></a>
		<?php if ($type == 'showphotos') { ?>
			<?php echo $fst->linkToAlbumsList($tag, 'View Albums', 'id="buttonleft" class="button"'); ?>
		<?php } ?>
		<div id="title">
			<h1 id="titlename"> </h1>
			<span id="albumnumber"> </span>
		</div>
	</div>
	<div id="secondbar">
		<?php 
		if ($type == 'showalbums') { 
			$tagarray = explode(' ', FST_ALLOW_TAGS);
			if (count($tagarray) > 1) {
		?>
		<ul id="tagbar">
			<?php foreach($tagarray as $tag) { ?>
			<li><a id="t-<?php echo $tag; ?>" href="#<?php echo $tag; ?>"><?php echo $tag; ?></a></li>
			<?php } // end foreach() ?>
		</ul>
		<?php 
			} // end if count()
		} // end if $type
		?>
	</div>
</div> <!-- end #header -->

<div id="container">
	<!-- images must not be generated from js: issues with history and with safari -->
	<?php 
	if ($type == 'showphotos') {
		foreach($album['photo'] as $photo) { 
			$desc = ($photo['description']) ? ' desc' : '';
	?>
			<div id="b-<?php echo $fst->photoUniqueId($photo); ?>" class="block clickable"><div class="title<?php echo $desc; ?>"> </div><div class="wrapper"><img src="<?php echo $fst->photoUrl($photo, 'm'); ?>" class="reflect rheight30 ropacity25" alt="" /></div></div>
	<?php 
		} // end foreach() 
	}
	else {
		// should always set to default tag: js will take care of other tags
		$albums = $fst->getAlbumsListInTag();
		foreach($albums as $album) {
			$photo = array(
				'id' 		=> $album['primary'],
				'server' 	=> $album['server'],
				'secret' 	=> $album['secret'],
				'title' 	=> $album['description'],
				'farm'		=> $album['farm']
			);
	?>
			<div id="a-<?php echo $album['id']; ?>" class="albumblock clickable <?php echo $album['tags']; ?>">
				<div class="albumdate"><?php echo $fst->albumDate('M Y', $album); ?></div>
				<div class="photonum"><?php echo $album['total']; ?></div>
				<div class="primaryphoto" style="background-image: url(<?php echo $fst->photoUrl($photo, 'm'); ?>);"></div>
				<div class="albumtitle"><?php echo htmlspecialchars($album['title']); ?></div>
			</div>
	<?
		}
	} // end if/else
	?>
</div> <!-- end #container -->

<?php if ($type == 'showphotos') { ?>
<div id="mainphoto" class="darkbox" style="display:none;">
	<div id="h-title" class="title"> </div>
	<div id="centerpiece"> </div>
	<div id="h-description" class="description"> </div>
	<div id="hud" class="notclickable">
		<a id="hud-prev" href="#"></a>
		<a id="hud-next" href="#"></a>
	</div>
</div> <!-- end #mainphoto -->
<?php } ?>

<!-- uncomment this if using google analytics 
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
-->
<script type="text/javascript">
_uacct = "<?php echo WEB_ANALYTICS; ?>";
</script>

</body>
</html>